package com.example.springstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springstudy.dao.dto.SkuDto;
import com.example.springstudy.dao.dto.SkuExportDto;
import com.example.springstudy.dao.res.SkuListRes;
import com.example.springstudy.entity.Sku;
import com.example.springstudy.mapper.SkuMapper;
import com.example.springstudy.service.SkuService;
import com.example.springstudy.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

@Slf4j
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {

    @Autowired
    SkuMapper skuMapper;

    @Override
    public SkuListRes getSkuList(int page, int size, String keyword) {
        LambdaQueryWrapper<Sku> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Sku::getId).eq(Sku::getIsDeleted, 0).like(Sku::getName, keyword);
        int totalCount = (int)this.count(wrapper);
        List<Sku> skuList = this.list(new Page<Sku>(page, size), wrapper);
        return new SkuListRes(skuList, page, size, totalCount);
    }

    @Override
    public Sku addSku(Sku sku) {
        return this.save(sku)?sku:null;
    }

    @Override
    public Sku updateSku(Sku sku) {
        return this.updateById(sku)?sku:null;
    }

    @Override
    public boolean addSkuListByExcel(List<Sku> list) {
        return false;
    }

    @Override
    public List<Sku> listAll(String keyword) {
        LambdaQueryWrapper<Sku> wrapper = new LambdaQueryWrapper<Sku>();
        wrapper.eq(Sku::getName, keyword);
        return this.list(wrapper);
    }

    @Override
    public Sku getOneByCode(String code) {
        return this.getOne(new LambdaQueryWrapper<Sku>().eq(Sku::getCode, code));
    }

    @Override
    public List<SkuExportDto> findExportList(List<Integer> ids) {
        return this.baseMapper.findExportSku(ids);
    }

    @Transactional
    @Override
    public boolean updateSkuCategory(int categoryId, List<Integer> skuIds) {
        //先查询在sku_conn_category表中categoryId已存在的skuId,这些id不用处理，没有则添加
        LambdaQueryWrapper<Sku> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Sku::getId, skuIds);
        List<Sku> list = this.list(wrapper);
        list.removeIf(sku -> sku.getType()==categoryId);
        list.forEach(item->item.setType(categoryId));
        return this.updateBatchById(list);

//        List<Integer> findIds = skuMapper.findCategorySkuList(categoryId, skuIds);
//        skuIds.removeAll(findIds);
//        log.info("findIds:{}, skuIds: {}", findIds, skuIds);
//        return skuMapper.saveSkuCategory(categoryId, skuIds);
    }

    @Override
    public int updateSkuStocks(List<Sku> skus, int compute) throws RuntimeException {
        int count = 0;
        for(Sku sku : skus) {
            Sku serverSku = this.getById(sku.getId());
            if(compute == -1){
                serverSku.setStock(serverSku.getStock() - sku.getStock());
            } else {
                serverSku.setStock(serverSku.getStock() + sku.getStock());
            }
            if(serverSku.getStock()<0){
                throw new RuntimeException(serverSku.getName()+"库存不足！");
            }
            if(this.updateById(serverSku)){
                count ++;
            }
        }
        return count;
    }
}
